package com.example.aiodemo.test;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntConsumer;
import java.util.function.Supplier;

public class DemoSupplier {
    public static void main(String[] args) throws Throwable {
        int[] arr = {12, 68, 10, 2, 99, 313, 46};
        // 调用 getMax 方法获得最大值，Lambda 相当于方法体
        int num = getMax(() -> {
            int max = arr[0];
            for (int i = 1; i < arr.length; i++) {
                if (max < arr[i]) {
                    max = arr[i];
                }
            }
            return max;
        });
        int n = getMax(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int max = arr[0];
                for (int i = 1; i < arr.length; i++) {
                    if (max < arr[i]) {
                        max = arr[i];
                    }
                }
                return max;
            }
        });
        //输出最大值
        System.out.println("最大值是：" + num);

        List<String> list = Arrays.asList("1", "2", "3");
        list.forEach(System.out::println);
        for (String s : list
        ) {
            System.out.println(1 + s);
        }
        Consumer<Object> runnable = System.out::println;
        runnable.accept("1");
        list.stream().filter(s -> s.contains("1")).forEach(System.out::println);
        Function<Supplier<Integer>, Integer> getMax = DemoSupplier::getMax;
        Integer apply = getMax.apply(() -> 1);
        int[] arr2 = new int[5];
        System.out.println(arr2[4]);
        for (int i = 0; i <= arr2.length; i++) {
            try {

                System.out.println(arr2[i]);
            } catch (Exception e) {
                System.out.println("数组越界");
            }
        }
        List<Throwable> list1 = new ArrayList<>();
        list1.add(new Exception("1"));
        list1 = null;
        try {

            System.out.println(list1.get(0));
        } catch (Exception e) {
            System.out.println("空指针异常");
        } finally {
            System.out.println("finally");
        }
       /* for (int i = 0; i < 99999999; i++) {
     *//*       try {*//*
                List<DemoSupplier> list2 = new ArrayList<>(999999999);
            *//*    System.out.println(i);
            } catch (Error e) {
                System.out.println(i);
            }*//*

        }*/

    }

    //使用 Supplier 做为参数
    public static int getMax(Supplier<Integer> supplier) {
        return supplier.get();
    }

    public int i(int i) {
        if (i == 1)
            throw new ArrayIndexOutOfBoundsException();
        return 1;
    }

    private ArrayList<String> l = new ArrayList<>(999999999);
}
